using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameOnCopyBook.Time;
using GameOnCopyBook.Rendering;
using InputManagement;


namespace GameOnCopyBook.Menu
{
    /// <summary>
    /// Questo è un componente del gioco che implementa IUpdateable.
    /// </summary>
    public class GameOver : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        ScoreManager score = new ScoreManager();
        BestTime bestScore = new BestTime();
        BestTime currentScore = new BestTime();
        RenderingData renderingData;

        public GameOver(Game game)
            : base(game)
        {
            // TODO: creare qui eventuali componenti figlio
            renderingData = Game.Services.GetService(typeof(RenderingData)) as RenderingData;    
        }

        /// <summary>
        /// Consente al componente del gioco di eseguire l'inizializzazione necessaria prima dell'inizio
        /// dell'esecuzione. Qui è possibile eseguire query per eventuali servizi necessari e caricare contenuto.
        /// </summary>
        public override void Initialize()
        {
            // TODO: aggiungere qui il codice di inizializzazione
            Menu.soundMgr.PlayInPause();
            base.Initialize();
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(RenderingData));
            base.Dispose(disposing);
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            renderingData.gameOverTexture = Game.Content.Load<Texture2D>(@"Textures\background3");
            renderingData.font = Game.Content.Load<SpriteFont>("SpriteFont1");
            renderingData.font2 = Game.Content.Load<SpriteFont>("SpriteFont2");
            bestScore.Record = score.LoadBestTime();
            currentScore.Record = GameLogic.game_state.BestTime;
                       

            base.LoadContent();
        }

        /// <summary>
        /// Consente al componente del gioco di aggiornare se stesso.
        /// </summary>
        /// <param name="gameTime">Fornisce uno snapshot dei valori di temporizzazione.</param>
        public override void Update(GameTime gameTime)
        {
            if (currentScore.Record > bestScore.Record)
            {
                score.SaveBestTime(currentScore.Record);
            }
            else
            {
                score.SaveBestTime(bestScore.Record);
            }
            // TODO: aggiungere qui il codice di aggiornamento
            if (InputManager.GetState().restart)
            {
                while (Game.Components.Count > 0)
                {
                    ((GameComponent)Game.Components[0]).Dispose();
                }

                Game.Components.Clear();

                Game.Components.Add(new InputManager(Game));
                Game.Components.Add(new Logic.Logic(Game));
                Game.Components.Add(new Rendering.Rendering(Game));
                GameLogic.resetState("Square");
                GameLogic.game_state.Timer = 0;
            }

            if (InputManager.GetState().quit)
            {
                while (Game.Components.Count > 0)
                {
                    ((GameComponent)Game.Components[0]).Dispose();
                }

                Game.Components.Clear();

                Game.Components.Add(new InputManager(Game));
                Game.Components.Add(new Rendering.Rendering(Game));
                Game.Components.Add(new Menu(Game));
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(renderingData.gameOverTexture, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
                        
            spriteBatch.DrawString(renderingData.font2, "GAME OVER! ", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f - 150.0f, 100.0f), Color.Black, 0.0f, renderingData.font.MeasureString("GAME OVER! ")/2.0f, 1.0f, SpriteEffects.None, 0.5f);
            spriteBatch.DrawString(renderingData.font, "Current Time: " + currentScore.Record + "sec", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f, 300.0f), Color.Black, 0.0f, renderingData.font.MeasureString("Current Time: -- sec ") / 2.0f, 1.0f, SpriteEffects.None, 0.5f);
            spriteBatch.DrawString(renderingData.font, "Best Score: " + bestScore.Record + "sec", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f, 400.0f), Color.Black, 0.0f, renderingData.font.MeasureString("best Time: -- sec ") / 2.0f, 1.0f, SpriteEffects.None, 0.5f);
            spriteBatch.DrawString(renderingData.font, "R to Restart, Q to Quit", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f + 50.0f, 500.0f), Color.Black, 0.0f, renderingData.font.MeasureString(" Click or R to Restart, Q to Quit") / 2.0f, 1.0f, SpriteEffects.None, 0.5f);
            
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
